//每个正则是用&&来分割，正则对应消息也使用&&分割
jQuery.fn.valid = function(newParams){
    var defaultParams = {
        callback:function(msg,elm){
            console.log(msg)
        },
        beforesubmit:function(msg){
            console.log(msg)
        },
        isAllCheck:false
    }

    var param = Object.assign(defaultParams, newParams)
    console.log(param)
    var a = this[0]
    var temparr = {}
    var checkallarr = []
    var validPower = 1//其中一项不通过减一
    var requiredReg = new RegExp('required','i')
    var dataRegReg = new RegExp('dataReg','i')
    var checkGroupReg = new RegExp('checkGroup','i')
    var checkSingle = new RegExp('checkSingle','i')
    
    this.submit(function(e){
        console.log('提交前')
        console.log('validPower'+validPower)
        if(validPower<1){
            $(this).unbind();//销毁绑定的提交事件，避免多个提交事件绑定
            return false
        }else{
            if(!param.Ajax){
                param.beforesubmit('ok'); 
                return true
            }else{
                return false
            }
        }
    })
    
    for(var i = 0;i<a.length;i++ ){
        if($(a[i]).attr('required')){
            $(a[i]).attr('novalidate','true')//禁用默认验证样式
            temparr['required'+i] = a[i]
        }
        if($(a[i]).attr('data-reg')){
            temparr['dataReg'+i] = a[i]
        }
        if($(a[i]).attr('type')=='checkbox'&&$(a[i]).attr('check-class')){
            var name = $(a[i]).attr('check-class')
            if(typeof(temparr['checkGroup'+name])=='undefined'){
                temparr['checkGroup'+name] = []
            }
            temparr['checkGroup'+name].push(a[i])
        }
        if($(a[i]).attr('type')=='checkbox'&&$(a[i]).attr('check-single')){
            temparr['checkSingle'+i] = a[i]
        }
    }
    console.log(temparr)

    for(var t in temparr){
        var inputElm = temparr[t]

        // required的
        if(requiredReg.test(t)){
            if($(inputElm).val()==''){
                var requiredMsg = $(inputElm).attr('required-msg')
                param.callback(requiredMsg,inputElm)
                validPower--
                if(!param.isAllCheck){
                    return this
                }
            }
        }

        // data-reg的
        if(dataRegReg.test(t)){
            var regstr = $(inputElm).attr('data-reg')
            var regmsg = $(inputElm).attr('reg-msg')
            //分离每个正则
            var fengereg = new RegExp("&&","gi")
            if(fengereg.test(regstr)){
                var regarr = regstr.split('&&')
                var regMsgArr = regmsg.split('&&')
                //逐个正则匹配，不匹配则执行回调函数并跳出

                var tempPower = 1
                $(regarr).each(function(i,r){
                    var reg = new RegExp(r,'i')
                    var val = $(inputElm).val()
                    console.log(reg)
                    console.log(val)
                    if(!reg.test(val)){
                        param.callback(regMsgArr[i],inputElm)
                        validPower--
                        tempPower--
                        if(tempPower<1){return false}//检验不通过，里面的循环跳出，外面循环跟住跳出
                    }
                })

                if(tempPower<1&&!param.isAllCheck){
                    return this
                }
            }else{
                var regsingle = new RegExp(regstr,'i')
                if(!regsingle.test($(this).val())){
                    param.callback(regmsg,this)
                    validPower--
                    if(!param.isAllCheck){
                        return this
                    }
                }
            }
        }

        // checkGroup的
        if(checkGroupReg.test(t)){
            var msgswitch = 0;
            $(inputElm).each(function(index,elm){
                if($(elm).prop('checked')){msgswitch++}
            })

            if(msgswitch==0){
                var checkedMsg = '至少勾选'+$(inputElm[0]).attr('check-class')+'中的一项';
                param.callback(checkedMsg,inputElm)
                validPower--
                if(!param.isAllCheck){
                    return this
                }
            }
        }

        //checkSingle的
        if(checkSingle.test(t)){
            var checkSingleMsg = $(inputElm).attr('check-single-msg')
            if(!$(inputElm).prop('checked')){
                param.callback(checkSingleMsg,inputElm);
                validPower--
                if(!param.isAllCheck){
                    return this
                }
            }
        }

    }

    //自带返回值
    if(validPower<1){return false}else{return true}


}
